package br.uvv.sao.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.uvv.sao.domain.Perfil;
import br.uvv.sao.domain.Professor;
import br.uvv.sao.enums.SituUsuario;
import br.uvv.sao.util.HibernateUtil;

public class ProfessorDAO extends DAO {
	
	private static final long serialVersionUID = 3427794905504675390L;

	public Boolean salvar(Professor professorInstance) {
		try{
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			this.sessao.save(professorInstance);
			this.transacao.commit();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public Boolean atualizar(Professor professorInstance) {
		try{
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			this.sessao.update(professorInstance);
			this.transacao.commit();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public void excluir(Professor professorInstance) {
		try{
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			this.sessao.delete(professorInstance);
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Professor> listar(String nome, String matricula) {
		List<Professor> professorList = null;
		try {
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(Professor.class);
			filtro.add(Restrictions.ilike("nomeCompleto", "%" + nome + "%"));
			filtro.add(Restrictions.ilike("codigo", "%" + matricula + "%"));
			filtro.add(Restrictions.eq("situacao", SituUsuario.Ativo));
			filtro.addOrder(Order.asc("nomeCompleto"));
			professorList = (ArrayList<Professor>) filtro.list();
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
		return professorList;
	}
	
	public Professor get(Long id) {
		Professor professorInstance= null;
		try {
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(Professor.class);
			filtro.add(Restrictions.eq("id", id));
			filtro.addOrder(Order.asc("id"));
			professorInstance = (Professor) filtro.uniqueResult();
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
		return professorInstance;
	}
	
	@SuppressWarnings("unchecked")
	public List<Professor> listar() {
		List<Professor> professorList = null;
		try {
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(Professor.class);
			filtro.add(Restrictions.eq("situacao", SituUsuario.Ativo));
			filtro.addOrder(Order.asc("id"));
			professorList = (ArrayList<Professor>) filtro.list();
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
		return professorList;
	}
	
	public Professor findByObject(String campo, String valor) {
		Professor professorInstance = new Professor();
		try{
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(Professor.class);
			filtro.add(Restrictions.eq(campo, valor));
			filtro.setMaxResults(1);
			professorInstance = (Professor) filtro.uniqueResult();
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
		return professorInstance;
	}
	
	public Integer findByObjectCount(String campo, String valor) {
		Integer count = 0;
		try{
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(Professor.class);
			filtro.add(Restrictions.eq(campo, valor));
			count = filtro.list().size();
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
		return count;
	}
	
	public Long listarUltimoIndice(){
		Long retorno = (long) 0;
		try{
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(Professor.class);
			filtro.setMaxResults(1);
			filtro.addOrder(Order.desc("id"));
			Professor professorInstance = (Professor) filtro.uniqueResult();
			retorno = professorInstance.getId();
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
		return retorno;
	}

	public Perfil getPerfilProfessor() {
		Perfil perfilInstance = new Perfil();
		try{
			this.sessao = HibernateUtil.getSessionfactory().getCurrentSession();
			this.transacao = this.sessao.beginTransaction();
			Criteria filtro = this.sessao.createCriteria(Perfil.class);
			filtro.add(Restrictions.eq("nome", "Professor"));
			filtro.setMaxResults(1);
			perfilInstance = (Perfil) filtro.uniqueResult();
			this.transacao.commit();
		} catch(Exception e){
			e.printStackTrace();
		}
		return perfilInstance;
	}
}